package net.huashitong.synthetical.TaskJob.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.AddressBook.entity.AddressBook;
import net.huashitong.synthetical.AddressBook.filter.BookFilter;
import net.huashitong.synthetical.AddressBook.service.AddressBookService;
import net.huashitong.synthetical.TaskJob.Entity.TaskJob;
import net.huashitong.synthetical.TaskJob.dao.TaskJobDao;
import net.huashitong.synthetical.TaskJob.filter.TaskJobFilter;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class TaskJobService extends StringPKService<TaskJob> {

   @Autowired
   private TaskJobDao dao;
   @Autowired
   private SysTableService sysTableService;

   @Autowired
   private AddressBookService bookService;

    @Override
    protected BaseDao<TaskJob, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(TaskJob obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_task_job",null));
    }


    /**
     * 查询任务主要入口
     * @param filter
     * @return
     */
    public List<TaskJob> getTaskLists(TaskJobFilter filter)
    {
        return dao.getMissinTask(filter);
    }


    /**
     *   ------------保存手动新建任务-----------
     */

    public void saveTaskJob(TaskJobFilter filter)
    {
        TaskJob task  = new TaskJob();
        BeanUtils.copyProperties(filter,task);
        task.setTasktimes("1"); //督查次数默认1
        task.setAaa001("1");
        task.setTaskResult("0"); //责任单位完成情况默认为0
        task.setSupervisionResult("0");//督查组完成情况默认为0
        task.setRespResult("0");//责任单位完成情况默认为0
        task.setPlanStartDate(UtilsTools.getCurrentDate()); // 任务开始时间默认为当前日期
        task.setPlanEndDate(filter.getPlanStartDate() ); // 任务开始时间默认为当前日期
        task.setIsfeedback("0");//是否反馈攻坚办
        task.setTaskDate(StringUtils.isBlank(filter.getTaskDate())?"15":filter.getTaskDate()   );
        task.setSource("手动创建");
        task.setPlanEndDate(filter.getPlanEndDate());
       /* try {
           // task.setPlanEndDate(UtilsTools.plusDay(Integer.parseInt(task.getTaskDate()),UtilsTools.getCurrentDate())); //设置结束时间
        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        save(task);

    }



    /**
     * -------------------修改任务--------------
     * @param filter
     * @param task
     */
    public void updateTaskJob(TaskJobFilter filter,TaskJob task)
    {
        // NodeTaskRecord nodeRecord  = new NodeTaskRecord();
        // nodeRecord.setSource(task.getTaskStage());
        BeanUtils.copyProperties(filter,task);
        // nodeRecord.setTarget();
        update(task);

    }

    /****
     *
     * -------------项目立项 ------------
     */
    public TaskJob  saveApprovalItem(TaskJobFilter filter)
    {
        TaskJob task  = new TaskJob();
        BeanUtils.copyProperties(filter,task);
        task.setTasktimes("1"); //督查次数默认1
        task.setAaa001("1");
        task.setTaskResult("0"); //  是否办结
        task.setSupervisionResult("0");//督查组完成情况默认为0
        task.setRespResult("0");//责任单位完成情况默认为0
        task.setPlanStartDate(UtilsTools.getCurrentDate()); // 任务开始时间默认为当前日期
        task.setPlanEndDate( filter.getPlanEndDate()); // 任务开始时间默认为当前日期
        task.setIsfeedback("0");//是否反馈攻坚办
        task.setTaskDate(StringUtils.isBlank(filter.getTaskDate())?"15":filter.getTaskDate()   );
        task.setSource("手动创建");
        task.setStartDate(UtilsTools.getCurrentDate());
        task.setIsScore(filter.getIsScore());  //是否绩效考核
        task.setTaskclas("1"); //1 项目立项， 2 督查任务
       /* try {
           // task.setPlanEndDate(UtilsTools.plusDay(Integer.parseInt(task.getTaskDate()),UtilsTools.getCurrentDate())); //设置结束时间
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        BookFilter bf  =new BookFilter();
        bf.setDeptCode(filter.getResDept());
       // bf.setDeptType();
        AddressBook book  = bookService.getBookObj(bf);
        task.setRespCode(ObjectUtils.isNotBlank(book)?book.getDeptCode():null);
        task.setResDept(ObjectUtils.isNotBlank(book)?book.getDeptName():null);
        save(task);

        return task;
    }


}